//
//  MainView.m
//  INFSWA02
//
//  Created by Justin van Grootveld on 9/26/11.
//  Copyright 2011 Justinform. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "MainViewController.h"
#import "Board.h"
#import "Piece.h"
#import "Player.h"
#import "Square.h"
#import "defaultBoardFactory.h"
#import "TestBoardFactory.h"

@interface MainViewController(Local)
- (void)initialize;
- (void)buttonClicked:(NSButton *)button;
- (void)undoButtonClicked:(NSButton *)button;
- (NSPoint)pointFromTag:(long)tag;
@end

@implementation MainViewController

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) 
    {
        [self initialize];
    }
    
    return self;
}

- (void)initialize
{
    _board = [DefaultBoardFactory createBoardWithPlayer1:@"player1" andPlayer2:@"player2"];
//     _board = [TestBoardFactory createBoardWithPlayer1:@"player1" andPlayer2:@"player2"];
    [_board setMainViewController:self];
    
#define START 50
#define SIZE 55
#define SPACE 0
    
    //add grid
    for(NSMutableArray *row in _board.grid)
    {
        NSMutableArray *buttonRow = [[NSMutableArray alloc] initWithCapacity:row.count];
        
        for(Square *square in row)
        {
            NSPoint point = square.location;
            NSInteger x = START + (point.x * SIZE) + (point.x * SPACE);
            NSInteger y = START + (point.y * SIZE) + (point.y * SPACE);
            
            NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(x, y, SIZE, SIZE)];
            [button setBezelStyle:NSThickSquareBezelStyle];
            
            [button setTag:(((int)point.x * 10) + (int)point.y)];
            [button setTarget: self];
            [button setAction: @selector(buttonClicked:)];

            [square setButton:button];
            [buttonRow addObject:button];
            [self addSubview:button];
        }
        
        [_buttonGrid addObject:buttonRow];
    }
    
    NSButton *undoButton = [[NSButton alloc] initWithFrame:NSMakeRect(10, 10, 60, 30)];
    [undoButton setBezelStyle:NSThickSquareBezelStyle];
    [undoButton setTitle:@"Undo"];

    [undoButton setTarget: self];
    [undoButton setAction: @selector(undoButtonClicked:)];
    
    [self addSubview:undoButton];
    
    [_board updateButtonTitles];
}

- (void)buttonClicked:(NSButton *)button
{
    NSPoint tag = [self pointFromTag:button.tag];
    
//    NSLog(@"%@", [[_board squareAtLocationX:tag.x andY:tag.y] piece]);
    [_board.currentState didSelectSquare:[_board squareAtLocationX:tag.x andY:tag.y] onBoard:_board];
}

- (void)undoButtonClicked:(NSButton *)button
{
    [_board loadState];
}

- (void)setColor:(NSColor *)color forSquares:(NSMutableArray *)squares
{
    for(NSView *view in self.subviews)
    {
        if([view isKindOfClass:[NSButton class]])
        {
            NSButton *button = (NSButton *)view;
            NSPoint tag = [self pointFromTag:button.tag];
            
            for(Square *square in squares)
            {
                if(square.location.x == tag.x && square.location.y == tag.y) 
                {
                    if([square hasPiece])
                    {
                        [button setTextColor:[NSColor redColor]];
                    }
                    else
                    {
                        [button setTitle:@"*"];
                    }
                }
            }
        }
    }
}

- (void)clearMarks
{
    [_board updateButtonTitles];
}

//only for grid max to 9-9
- (NSPoint)pointFromTag:(long)tag
{
    NSPoint returnPoint;
    NSInteger length = [[NSString stringWithFormat:@"%dl", tag] length];
    
    if(length == 2)//1 number
    {
        returnPoint.x = 0;
        returnPoint.y = tag;
    }
    else//2 numbers
    {
        returnPoint.x = (int)(tag / 10);
        returnPoint.y = (tag % 10);
    }
    
    return returnPoint;
}

- (void)drawRect:(NSRect)rect
{
    //background
    [[NSColor whiteColor] set];
    
    NSRectFill(rect);
}

@end
